#include "Presence.h"




Presence(Type type) :
	type(type)
{
}


Presence(Type type, std::string & status, int priority, Mode mode) :
	type(type),
	status(status),
	priority(priority),
	mode(mode)
{
}


bool isAvailable() {
	return type == TYPE_AVAILABLE;
}


bool isAway() {
	return type == TYPE_AVAILABLE && (mode == MODE_AWAY || mode == MODE_XA || mode == MODE_DND); 
}


std::string toXML() {
	std::string buf("<presence");
    if(!getXmlns().empty()) {
		buf.append(" xmlns=\"").append(getXmlns()).append("\"");
	}
	if (!language.empty()) {
		buf.append(" xml:lang=\"").append(getLanguage()).append("\"");
	}
	if (!getPacketID().empty()) {
		buf.append(" id=\"").append(getPacketID()).append("\"");
	}
	if (!getTo().empty()) {
		buf.append(" to=\"").append(StringUtils::escapeForXML(getTo())).append("\"");
	}
	if (!getFrom().empty()) {
		buf.append(" from=\"").append(StringUtils::escapeForXML(getFrom())).append("\"");
	}
	if (type != available) {
		buf.append(" type=\"").append(type).append("\"");
	}
	buf.append(">");
	if (!status.empty()) {
		buf.append("<status>").append(StringUtils::escapeForXML(status)).append("</status>");
	}
	if (priority != 0) {
		buf.append("<priority>").append(priority).append("</priority>");
	}
	if (mode != undefined && mode != available) {
		buf.append("<show>").append(mode).append("</show>");
	}

	buf.append(getExtensionsXML());

	// Add the error sub-packet, if there is one.
	XMPPError * error = getError();
	if (error != NULL) {
		buf.append(error->toXML());
	}

	buf.append("</presence>");

	return buf;
}



